<template>

    <el-dialog :title="id == null ? '添加' : '编辑'" :visible.sync="dialogVisible" :close-on-click-modal="false">

      <el-form ref="main-form" status-icon :model="mainQuery" :rules="rules" label-width="120px" label-position="left" :inline="false" style="width: 400px;">

        <el-form-item label="ID" size="mini" v-if="id != null">
          <el-input placeholder="" onfocus="this.select()" v-model="id" readonly></el-input>
        </el-form-item>

        <el-form-item label="物流公司简称" size="mini" prop="shortName">
          <el-input placeholder="" v-model="mainQuery.shortName" @keyup.enter.native="submit" clearable></el-input>
        </el-form-item>
        <el-form-item label="物流公司全称" size="mini" prop="fullName">
          <el-input placeholder="" v-model="mainQuery.fullName" @keyup.enter.native="submit" clearable></el-input>
        </el-form-item>
        <el-form-item label="运单查询网址" size="mini" prop="website">
          <el-input placeholder="" v-model="mainQuery.website" @keyup.enter.native="submit" clearable></el-input>
        </el-form-item>

        <el-form-item label="修改时间" size="mini" v-if="data != null">
          <span>{{data.updateTime}}</span>
        </el-form-item>
        <el-form-item label="创建时间" size="mini" v-if="data != null">
          <span>{{data.createTime}}</span>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">

        <el-popover
          v-if="id != null"
          placement="top"
          width="160"
          v-model="popDeleteVisible">
          <p>确定删除吗？</p>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="default" @click="popDeleteVisible = false">取消</el-button>
            <el-button type="danger" size="mini" @click="submitDel">确定</el-button>
          </div>
          <el-button type="danger" slot="reference"  size="small" style="float: left;" :loading="loading">删 除</el-button>
        </el-popover>

        <el-button @click="close" size="mini">取 消</el-button>
        <el-button @click="submit" size="mini" type="primary" :loading="loading">提 交</el-button>
      </div>
    </el-dialog>

</template>

<script>

import ApiMain from '@/api/logistics.company';

export default {
  mixins: [
  ],
  props: [
    'statusOptions',
  ],
  data () {
    return {

      dialogVisible   : false,
      popDeleteVisible: false,
      loading         : false,

      mainQueryRaw: null,
      mainQuery: {
        shortName: '',
        fullName: '',
        website: '',
      },

      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' },
        ],
      },

      id  : null,
      data: null,
    }
  },
  mounted () {
    this.mainQueryRaw = Object.copy(this.mainQuery);
  },

  methods: {

    show (id = null) {

      this.reset();

      if (id) {
        this.id = id;
        this.fetchData();
      }

      this.dialogVisible = true;
    },

    close () {
      this.dialogVisible = false;
      this.$emit('dialog-close');
    },

    reset () {
      this.mainQuery = Object.copy(this.mainQueryRaw);
      this.$refs['main-form'] && this.$refs['main-form'].resetFields();
      this.id   = null;
      this.data = null;
    },

    async fetchData () {

      try {
        var re = await ApiMain.info({id: this.id});
        this.data = re; 
      } catch (err) {
        this.data = null;
      }

      if (this.data) {
        this.mainQuery.shortName = this.data.shortName;
        this.mainQuery.fullName  = this.data.fullName;
        this.mainQuery.website   = this.data.website;
      }

    },

    verifyForm () {

      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit () {

      if (! this.verifyForm()) return false;

      this.loading = true;

      try {
        this.id ? await this.submitSet() : await this.submitAdd();
      } catch (err) {}

      this.loading = false;
    },

    async submitAdd () {
        await ApiMain.add(this.mainQuery);
        this.$message({message: '添加成功', type: 'success'});
        this.close();
    },

    async submitSet () {
      var req = Object.copy(this.mainQuery);
      req.id = this.id;

      await ApiMain.set(req);
      this.$message({message: '修改成功', type: 'success'});
      this.close();
    },

    async submitDel () {
      this.popDeleteVisible = false;
      this.loading          = true;

      try {
        await ApiMain.del({id: this.id});
        this.$message({message: '删除成功', type: 'success'});
        this.close();
      } catch(e) {
      }

      this.loading = false;
    },
  }
}
</script>

<style>

</style>
